package com.jiuyin.dianjing.ui.activity.webview;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.jiuyin.dianjing.R;
import com.jiuyin.dianjing.ui.activity.base.BaseActivity;
import com.jiuyin.dianjing.view.RichWebView;
import com.jiuyin.dianjing.view.SlowlyProgressBar;

import butterknife.BindView;

import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TITLE;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_URL;

public class WebViewContainer extends BaseActivity {
    @BindView(R.id.web_container)
    RichWebView mWebView;

    private SlowlyProgressBar mSlowlyProgressBar;

    //资讯链接
    private String mUrl;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_webview;
    }

    @Override
    protected void initData() {
        mSlowlyProgressBar = new SlowlyProgressBar(findViewById(R.id.ProgressBar));
    }

    @SuppressLint("JavascriptInterface")
    @Override
    protected void initView() {
        Intent intent = getIntent();
        if (null != intent) {
            mUrl = intent.getStringExtra(KEY_URL);
            String tile = intent.getStringExtra(KEY_TITLE);
            setTitle(tile);
            mWebView.setWebViewClient(new WebViewClient() {
                @Override
                public void onPageStarted(WebView view, String url, Bitmap favicon) {
                    mSlowlyProgressBar.onProgressStart();
                }
            });
            mWebView.setWebChromeClient(new WebChromeClient() {
                @Override
                public void onProgressChanged(WebView view, int newProgress) {
                    mSlowlyProgressBar.onProgressChange(newProgress);
                }
            });
            WebSettings settings = mWebView.getSettings();
            setWebViewSettings(settings);
            mWebView.addJavascriptInterface(this, "androidProxy");
            mWebView.loadUrl(mUrl);
        }
    }

    @SuppressLint("JavascriptInterface")
    private void setWebViewSettings(WebSettings settings) {
        // 设置WebView支持JavaScript
        settings.setJavaScriptEnabled(true);
        //支持自动适配
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        settings.setSupportZoom(false);  //支持放大缩小
        settings.setBuiltInZoomControls(false); //显示缩放按钮
        settings.setBlockNetworkImage(false);// 把图片加载放在最后来加载渲染
        settings.setAllowFileAccess(true); // 允许访问文件
        settings.setSaveFormData(true);
        settings.setGeolocationEnabled(true);
        settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        settings.setDomStorageEnabled(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);/// 支持通过JS打开新窗口
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
    }
}
